package com.jjlin.chapter_1;

import java.util.Stack;
import java.util.Scanner;

public class CD6{
    Stack<Integer> stack1 = new Stack<>();//主栈
    Stack<Integer> stack2 = new Stack<>();//辅助栈

    public void add(Integer num){
        stack1.push(num);
    }

    public int peek(){
        if(stack2.isEmpty() && stack1.isEmpty())
            throw new RuntimeException("Your queue is empty！");
        if(!stack2.isEmpty())
            return stack2.peek();
        else{
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            return stack2.peek();
        }
    }

    public int poll(){
        if(stack2.isEmpty() && stack1.isEmpty())
            throw new RuntimeException("Your queue is empty！");
        if(!stack2.isEmpty())
            return stack2.pop();
        else{
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
            return stack2.pop();
        }
    }

    public static void CD6(String[] args){
        CD6 CD6 = new CD6();
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        String[] str = new String[count];
        sc.nextLine();
        for(int i = 0; i < count; i++)
            str[i] = sc.nextLine();
        for(int i = 0; i < count; i++){
            String[] line = str[i].split(" ");
            if("add".equals(line[0])){
                Integer num = Integer.parseInt(line[1]);
                CD6.add(num);
            }else if("poll".equals(line[0])){
                CD6.poll();
            }else
                System.out.println(CD6.peek());
        }
    }
}

